package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private SetmealDao setmealDao;

    //分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //使用分页查询插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //分页查询数据库
        Page<CheckGroup> page = checkGroupDao.page(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(), page.getResult());
    }

    //新增
    @Override
    @Transactional
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //向检查组中添加数据
        checkGroupDao.insert(checkGroup);
        //获取新增检查组的id
        Integer checkGroupId = checkGroup.getId();
        //获取回显检查项目的id
        for (Object checkItemId : checkitemIds) {
            //将id加入关联表
            checkGroupDao.insertBatch(checkGroupId, checkItemId);
        }
    }

    //删除检查组
    @Override
    @Transactional
    public void deleteById(Integer id) {
        //判断检查组是否在套餐中
        Integer count = checkGroupDao.countGroupById(id);
        if (count > 0) {
            throw new BusinessRuntimeException("删除失败，此项目组在套餐中");
        }
        //根据id删除检查组
        checkGroupDao.deleteById(id);
        //根据id删除关联表中的数据
        checkGroupDao.deleteByGroupId(id);
    }

    //回显检查组
    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.show(id);
        return checkGroup;
    }

    //回显检查项
    @Override
    public List<Integer> findCheckItemId(Integer id) {
        List<Integer> list = checkGroupDao.showCheckItem(id);
        return list;
    }

    //修改检查组
    @Override
    public void edit(CheckGroup checkGroup, Integer[] ids) {
        CheckGroup group = new CheckGroup();
        BeanUtils.copyProperties(checkGroup, group);
        //修改项目组基本信息
        checkGroupDao.updateById(group);

        //删除原有的检查项数据
        List<Integer> groupIds = Arrays.asList(group.getId());
        for (Integer groupId : groupIds) {
            checkGroupDao.deleteByGroupId(groupId);
        }

        //重新插入检查项
        for (Integer id : ids) {
            checkGroupDao.insertBatch(group.getId(),id);
        }
    }

    //回显套餐中的检查组
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    @Override
    public List<Integer> findChegroupId(Integer id) {
        List<Integer> list = setmealDao.showChegroup(id);
        return list;
    }
}
